package cn.hotel.servlet;

import cn.hotel.dao.Administrator;
import cn.hotel.service.AdministratorService;
import cn.hotel.service.AdministratorServiceImpl;
import cn.hotel.utlis.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/administrator")
public class AdministratorServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
        String appPath = request.getContextPath();
        AdministratorService administratorService = new AdministratorServiceImpl();

        String type = request.getParameter("type");
        type = type == null? "inquire" : type;
        switch (type){
            case "inquire":
                administratorInquire(request,response,administratorService);
                break;
            case "add":
                //跳转到tianjia页面,第一个页面第一次请求的时候没有数据，直接重定向出去，定向到第二个页面
                response.sendRedirect(appPath+"/logjsp/administratoradd.jsp");
                break;
            case "delete":
                administratorDelete(request,response,administratorService,out);
                break;
            case "alter":
                String name = request.getParameter("name");
                request.setAttribute("name",name);
                request.getRequestDispatcher("logjsp/administrstoralter.jsp").forward(request,response);
                break;
            case "save":
                //第二个页面发送请求时，才开始正常使用方法去处理数据响应请求
                administratorAdd(request,response,administratorService,out);
                break;
            case "transmit":
                transmits(request,response,administratorService,out);
                break;
        }
    }
    //修改
    private void transmits(HttpServletRequest request, HttpServletResponse response, AdministratorService administratorService, PrintWriter out) {
        String appPath = request.getContextPath();
        String memberJurisdictions = request.getParameter("memberJurisdiction");
        String doBusinessJurisdictions = request.getParameter("doBusinessJurisdiction");
        String goodsJurisdictions = request.getParameter("goodsJurisdiction");
        String manageJurisdictions = request.getParameter("manageJurisdiction");
        String logJurisdictions = request.getParameter("logJurisdiction");
        String financeJurisdictions = request.getParameter("financeJurisdiction");

        cn.hotel.pojo.Administrator administrator = new cn.hotel.pojo.Administrator();
        administrator.setMemberJurisdiction(StringUtil.strToInt(memberJurisdictions));
        administrator.setDoBusinessJurisdiction(StringUtil.strToInt(doBusinessJurisdictions));
        administrator.setGoodsJurisdiction(StringUtil.strToInt(goodsJurisdictions));
        administrator.setManageJurisdiction(StringUtil.strToInt(manageJurisdictions));
        administrator.setLogJurisdiction(StringUtil.strToInt(logJurisdictions));
        administrator.setFinanceJurisdiction(StringUtil.strToInt(financeJurisdictions));
        administrator.setAdministratorNumber(request.getParameter("name"));
        Map<String ,Object> objectMap = administratorService.changeMap(administrator);
        if("1".equals(objectMap.get("flag"))){
            out.print("<script>alert('修改成功');location.href='"+appPath+"/administrator?type=inquire'</script>");
        }else {
            out.print("<script>alert('修改失败');location.href='"+appPath+"/administrator?type=inquire'</script>");
        }
        out.flush();
    }

    //添加
    private void administratorAdd(HttpServletRequest request, HttpServletResponse response, AdministratorService administratorService, PrintWriter out) {
        String appPath = request.getContextPath();
        cn.hotel.pojo.Administrator administrator = new cn.hotel.pojo.Administrator();
        administrator.setAdministratorNumber(request.getParameter("administratorNumber"));
        administrator.setPassword(request.getParameter("password"));
        String memberJurisdiction = request.getParameter("memberJurisdiction");
        String doBusinessJurisdiction = request.getParameter("doBusinessJurisdiction");
        String goodsJurisdiction = request.getParameter("goodsJurisdiction");
        String manageJurisdiction = request.getParameter("manageJurisdiction");
        String logJurisdiction = request.getParameter("logJurisdiction");
        String financeJurisdiction = request.getParameter("financeJurisdiction");
        administrator.setMemberJurisdiction(StringUtil.strToInt(memberJurisdiction));
        administrator.setDoBusinessJurisdiction(StringUtil.strToInt(doBusinessJurisdiction));
        administrator.setGoodsJurisdiction(StringUtil.strToInt(goodsJurisdiction));
        administrator.setManageJurisdiction(StringUtil.strToInt(manageJurisdiction));
        administrator.setLogJurisdiction(StringUtil.strToInt(logJurisdiction));
        administrator.setFinanceJurisdiction(StringUtil.strToInt(financeJurisdiction));
        Map<String ,Object> data = administratorService.addAdministratorMap(administrator);
        if("1".equals(data.get("flag"))){
            out.print("<script>alert('添加成功');location.href='"+appPath+"/administrator?type=inquire'</script>");
        }else {
            out.print("<script>alert('添加失败');location.href='"+appPath+"/administrator?type=inquire'</script>");
        }
        out.flush();
    }

    //删除
    private void administratorDelete(HttpServletRequest request, HttpServletResponse response, AdministratorService administratorService, PrintWriter out) {
        String name = request.getParameter("name");
        String appPath = request.getContextPath();
        Map<String ,Object> data = administratorService.deleteAdministratorMap(name);
        if("1".equals(data.get("flag"))){
            out.print("<script>alert('删除成功');location.href='"+appPath+"/administrator?type=inquire'</script>");
        }else {
            out.print("<script>alert('删除失败');location.href='"+appPath+"/administrator?type=inquire';</>");
        }
        out.flush();
    }

    //查询
    private void administratorInquire(HttpServletRequest request, HttpServletResponse response, AdministratorService administratorService) throws ServletException, IOException {
        String name = request.getParameter("names");

        Map<String ,Object> data = administratorService.administratorMap(name);
        if("1".equals(data.get("flag"))){
            List<Administrator> administratorList = (List<Administrator>) data.get("data");
            request.setAttribute("administratorList",administratorList);
            request.getRequestDispatcher("logjsp/administrator.jsp").forward(request,response);
        }


    }
}
